home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc2 / bio11.lha / Bio / Bio.c < prev    next >
C/C++ Source or Header  |  1996-04-06  |  8KB  |  300 lines

  1. /* A Biorythm program made by Henrik (Procyon) Wetterström */
  2. /* For any reason contact me at:  hwefri92@tufvan.hv.se    */
  3. /*                                                         */
  4. /* This program was made after reading about  Biorythms in */
  5. /* the docs to Richard Smedley's "Biorythms v2.0". It made */
  6. /* realize how easy it was to make one myself, and here is */
  7. /* my result.  For sure can a lot  of optimizations can be */
  8. /* done, but I don't feel like fiddle any more with it.    */
  9. /* Consider this program and source code as public domain. */
  10. /* If you  have plenty of use of it  somehow,  would it be */
  11. /* fun  to hear some cheering words from you,  but you are */
  12. /* for sure welcome to send me some parallel supercomputer */
  13. /* for my thesis work. :-)                                 */
  14. /* Oh, well, there is only one catch, this source code or  */
  15. /* object code generated from it must not be  stored on a  */
  16. /* storage available from any Microsoft application or OS. */
  17. /* I bet this program is not  the most  excellent piece of */
  18. /* source code, but maybe it can be of some guidance for a */
  19. /* beginner.  No guarantees for whatsoever is given by the */
  20. /* the author.
  21. /* I heard a rumour about a forth, intuition curve,  which */
  22. /* I added to the program. I hope this was correct...      */
  23. /*
  24. To compile: 
  25. Standard SAS/C
  26.   sc bio.c CPU=68000 MATH=standard NOSTACKCHECK NOCHECKABORT NOMULTIPLEINCLUDES OPTIMIZE LINK NOWARNVOIDRETURN OPTIMIZERSIZE OPTIMIZERINLINELOCAL SMALLCODE SMALLDATA STRIPDEBUG OPTIMIZERSCHEDULER NOVERSION OPTIMIZERALIAS STRINGSECTION=near
  27. 68030/FPU SAS/C
  28.   sc bio.c CPU=68030 MATH=68881 NOSTACKCHECK NOCHECKABORT NOMULTIPLEINCLUDES OPTIMIZE LINK NOWARNVOIDRETURN OPTIMIZERSIZE OPTIMIZERINLINELOCAL SMALLCODE SMALLDATA STRIPDEBUG OPTIMIZERSCHEDULER NOVERSION OPTIMIZERALIAS STRINGSECTION=near
  29. GCC
  30.  gcc bio.c -s -O3 -o bio
  31. */
  32.  
  33.  
  34. #include <stdio.h>
  35. #include <sys/types.h>
  36. #include <time.h>
  37. #include <math.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40.  
  41. #include <exec/types.h>
  42. #include <intuition/intuition.h>
  43. #include <intuition/screens.h>
  44. #include <intuition/gadgetclass.h>
  45. #include <libraries/gadtools.h>
  46. #include <graphics/gfx.h>
  47. #include <graphics/regions.h>
  48.  
  49. #include <clib/exec_protos.h>
  50. #include <clib/dos_protos.h>
  51. #include <clib/intuition_protos.h>
  52. #include <clib/gadtools_protos.h>
  53. #include <clib/graphics_protos.h>
  54.  
  55. #define PCYCLE 23    /* Physical     */
  56. #define ECYCLE 28    /* Emotional    */
  57. #define ICYCLE 33    /* Intellectual */
  58. #define NCYCLE 38    /* Intuition    */
  59.  
  60. #define WINWDT 400
  61. #define WINHGT 210
  62.  
  63. typedef struct st_bio {
  64.     int pp,ep,ip,np;
  65.     char today[10];
  66.     char birth[10];
  67.     int days;
  68. } Bio;
  69.  
  70. void handle_events(struct Window *win);
  71.  
  72. struct Library *IntuitionBase;
  73. struct Library *GadToolsBase;
  74. struct Library *GfxBase;
  75. struct Window *win;
  76.  
  77. int wtop,wleft,wright,wbottom;    /* win offsets */
  78. int wox,woy,wx,wy;        /* plot area pos & size */
  79.  
  80. struct TextAttr fnt = { "topaz.font", 8, 0, 0, };
  81. struct NewGadget ng[] = {
  82.     0,1,  88,13, (UBYTE *)"Quit",    &fnt, 0, PLACETEXT_IN,   NULL, NULL,
  83.     0,15, 88,13, (UBYTE *)"YY/MM/DD", &fnt, 1, PLACETEXT_BELOW, NULL, NULL
  84. };
  85. struct Gadget *glist = NULL;
  86. struct Gadget *gads[2];
  87. struct Gadget *gad;
  88. struct Screen *scr;
  89. struct RastPort *rp;
  90. void *vi;
  91. Bio *bio;
  92.  
  93.  
  94. static Bio *get_bio(int y, int m, int d) {
  95.     const char *months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  96.     static Bio bio;
  97.     time_t tp1,tp0;
  98.     struct tm *t1,t0={0,0,0,0,0,0,-1,-1,-1};
  99.     int ticks=60;
  100.  
  101.     t0.tm_mday=d;
  102.     t0.tm_mon =m-1;
  103.     t0.tm_year=y;
  104.  
  105.     tp0=mktime(&t0);
  106.     tp1=time(0);
  107.     t1=localtime(&tp1);
  108.     bio.days=(int)(difftime(tp1,tp0)/(ticks*60*24));
  109.     bio.pp=bio.days % PCYCLE;
  110.     bio.ep=bio.days % ECYCLE;
  111.     bio.ip=bio.days % ICYCLE;
  112.     bio.np=bio.days % NCYCLE;
  113.     sprintf(bio.birth,"%d-%s-%d",t0.tm_year, months[t0.tm_mon], t0.tm_mday);
  114.     sprintf(bio.today,"%d-%s-%d",t1->tm_year, months[t1->tm_mon], t1->tm_mday);
  115.  
  116.     return &bio;
  117. }
  118.  
  119. int openlibs( void ) {
  120.     if( IntuitionBase=OpenLibrary("intuition.library",37L) ) {
  121.         if( GadToolsBase=OpenLibrary("gadtools.library",37L) ) {
  122.             if( GfxBase=OpenLibrary("graphics.library",37L) ) return 1;
  123.             CloseLibrary((struct Library *)GadToolsBase);
  124.         }
  125.         CloseLibrary((struct Library *)IntuitionBase);
  126.     }
  127.     return 0;
  128. }
  129.  
  130. void closelibs( void ) {
  131.     CloseLibrary((struct Library *)GadToolsBase);
  132.     CloseLibrary((struct Library *)IntuitionBase);
  133. }
  134.  
  135. int getvisual( void ) {
  136.     if(scr=LockPubScreen(NULL)) {
  137.         wtop   =scr->WBorTop+scr->BarHeight;
  138.         wleft  =scr->WBorLeft+scr->BarHBorder;
  139.         wright =scr->WBorRight;
  140.         wbottom=scr->WBorBottom;
  141.         if(vi=GetVisualInfo(scr,TAG_END)) return 1;
  142.         UnlockPubScreen(NULL,scr);
  143.     }
  144.     return 0;
  145. }
  146.  
  147. void print( char *s, int x, int y,int pen ) {
  148.     struct IntuiText it;
  149.     it.FrontPen=pen;
  150.     it.BackPen=0;
  151.     it.DrawMode=1;
  152.     it.LeftEdge=0;
  153.     it.TopEdge=0;
  154.     it.ITextFont=&fnt;
  155.     it.IText=s;
  156.     it.NextText=NULL;
  157.     PrintIText(rp,&it,wleft+x,wtop+y);
  158. }
  159.  
  160. void plot2( int cycle, int color ) {
  161.     int pp,x;
  162.     double a,da;
  163.     SetAPen(rp,color);
  164.     pp=((bio->days)-17) % cycle;
  165.     a=((double)pp/(double)cycle)*2.0*PI;
  166.     da=((34.0*2*PI)/cycle)/(wx-14);
  167.     Move(rp,wox+7,woy+(wy/2)-(int)(((wy/2)-10)*sin(a)));
  168.     for(x=0; x<(wx-14); x++,a+=da) {
  169.         Draw(rp, wox+7+x, woy+(wy/2)-(int)(((wy/2)-10)*sin(a)));
  170.     }
  171. }
  172.  
  173. void refresh(void) {
  174.     char ds[20];
  175.     char *s;
  176.     int y,m,d,i;
  177.     DrawBevelBox(rp,wleft+90,wtop+1,WINWDT-96,42+10,GT_VisualInfo,vi,GTBB_Recessed,1L,TAG_END);
  178.     DrawBevelBox(rp,wox-2,woy-1,wx+5,wy+3,GT_VisualInfo,vi,GTBB_Recessed,1L,TAG_END);
  179.     SetAPen(rp,0);
  180.     RectFill(rp,(SHORT)wox,(SHORT)woy,(SHORT)wox+wx,(SHORT)woy+wy);
  181.     SetAPen(rp,1);
  182.     Move(rp,wox,   woy+(wy/2));
  183.     Draw(rp,wox+wx,woy+(wy/2));
  184.     Move(rp,wox+(wx/2),woy);
  185.     Draw(rp,wox+(wx/2),woy+wy);
  186.     for(i=wox+7; i<(wox+wx); i+=wx/35) {
  187.         WritePixel(rp,i,woy-1+(wy/2));
  188.         WritePixel(rp,i,woy+1+(wy/2));
  189.     }
  190.     s=((struct StringInfo *)gad->SpecialInfo)->Buffer;
  191.     if(strlen(s)) {
  192.         y=atoi(s);
  193.         m=atoi(s+3);
  194.         d=atoi(s+6);
  195.         if( y<0 || m<1 || m>12 || d<1 || d>31 ) return;
  196.         bio=get_bio(y,m,d);
  197.         print( bio->today,98,7,1);
  198.         sprintf(ds,"%d days old     ",bio->days);
  199.         print( bio->birth,98,19,1);
  200.         print( ds,98,31,1);
  201.         print("Physical",260,31,1);
  202.         print("Emotional",260,7,7);
  203.         print("Intellectual",260,19,3);
  204.         print("Intuition",260,40,2);
  205.         plot2(PCYCLE,1);
  206.         plot2(ECYCLE,7);
  207.         plot2(ICYCLE,3);
  208.         plot2(NCYCLE,2);
  209.     }
  210. }
  211.  
  212. int openwin( void ) {
  213.     WORD zoom[4]={0,12,180,12};
  214.     if( openlibs() ) {
  215.         if( getvisual() ) {
  216.             if ( gad=CreateContext( &glist ) ) {
  217.                 ng[0].ng_VisualInfo=ng[1].ng_VisualInfo=vi;
  218.                 ng[0].ng_TopEdge+=wtop;
  219.                 ng[1].ng_TopEdge+=wtop;
  220.                 ng[0].ng_LeftEdge+=wleft;
  221.                 ng[1].ng_LeftEdge+=wleft;
  222.                 gads[0] = gad = CreateGadget(BUTTON_KIND, gad, &ng[0],TAG_END );
  223.                 gads[1] = gad = CreateGadget(STRING_KIND, gad, &ng[1], GTST_MaxChars,8,TAG_END );
  224.                 zoom[1]=zoom[3]=wtop-1;
  225.                 if( win=OpenWindowTags(NULL,
  226.                     WA_Title,    "Biorythms © Procyon",
  227.                     WA_Width,    WINWDT+wleft+wright,
  228.                     WA_Height,    WINHGT+wtop+wbottom,
  229.                     WA_IDCMP,    IDCMP_CLOSEWINDOW | BUTTONIDCMP | IDCMP_NEWSIZE,
  230.                     WA_Flags,    WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
  231.                     WA_Gadgets,    glist,
  232.                     WA_Zoom,    zoom,
  233.                     TAG_DONE,    NULL
  234.                 ) ) {
  235.                     rp=win->RPort;
  236.                     wox=wleft+2;
  237.                     woy=wtop+46+10;
  238.                     wx=WINWDT-11;
  239.                     wy=WINHGT-51-10;
  240.                     refresh();
  241.                     GT_RefreshWindow(win,NULL);
  242.                     UnlockPubScreen(NULL,scr);
  243.                     return 1;
  244.                 }
  245.                 if ( glist ) FreeGadgets( glist );
  246.  
  247.             }
  248.             UnlockPubScreen(NULL,scr);
  249.             FreeVisualInfo(vi);
  250.         }
  251.         closelibs();
  252.     }
  253.     return 0;
  254. }
  255.  
  256. void closewin( void ) {
  257.     CloseWindow(win);
  258.     FreeVisualInfo(vi);
  259.     if ( glist ) FreeGadgets( glist );
  260.     closelibs();
  261. }
  262.  
  263. int main(int argc, char **argv) {
  264.     if(openwin()) {
  265.         handle_events(win);
  266.     }
  267.     closewin();
  268.  
  269. }
  270.  
  271. void handle_events(struct Window *win) {
  272.     int go=1;
  273.     ULONG sig;
  274.     struct IntuiMessage *imsg;
  275.     struct Gadget *gad;
  276.     while(go) {
  277.         sig=Wait( (1L<<win->UserPort->mp_SigBit) | (1L<<12) );
  278.         if(sig&(1L<<12)) go=0;
  279.         else {
  280.             while( go && (imsg=GT_GetIMsg(win->UserPort))) {
  281.                 switch(imsg->Class) {
  282.                 case CLOSEWINDOW:
  283.                     go=0;
  284.                     break;
  285.                 case IDCMP_REFRESHWINDOW:
  286.                     GT_BeginRefresh(win);
  287.                     GT_EndRefresh(win,1);
  288.                     break;
  289.                 case IDCMP_GADGETUP:
  290.                     gad=(struct Gadget *)imsg->IAddress;
  291.                     if(gad->GadgetID==0) go=0;
  292.                     break;
  293.                 }
  294.                 if(go) refresh();
  295.                 GT_ReplyIMsg(imsg);
  296.             }
  297.         }
  298.     }
  299. }
  300.